package com.o2o.cleaning.month.platform.ebusiness_plat.nlp

import com.alibaba.fastjson.{JSON, JSONObject}
import com.mongodb.spark.MongoSpark
import com.mongodb.spark.config.ReadConfig
import com.mongodb.spark.rdd.MongoRDD
import org.apache.spark.rdd.RDD
import org.apache.spark.sql._
import org.bson.Document


object MongoDBData {

  def main(args: Array[String]): Unit = {

//    val database = "Jingdong"
    val database = "Taobao"

//    val collection = "jd_comments_detail"
    val collection = "taobao_detail_comment_2203"

    val source_path = "d://test111"

    val spark = SparkSession.builder()
      .appName("Kuaishou_webcast")
      .config("spark.debug.maxToStringFields", "2000")
      .config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
      .config("spark.sql.caseSensitive", "true")
      .master("local[*]")
      .getOrCreate()

    val sc = spark.sparkContext
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")
    sc.setLogLevel("ERROR")

    val readConfig = ReadConfig(Map(
      "spark.mongodb.input.uri" -> "mongodb://ob:O2Odata123!@ 192.168.0.203:27017/admin"
      , "spark.mongodb.input.database" -> s"${database}"
      , "spark.mongodb.input.collection" -> s"${collection}"))

    // 加载mongodb配置信息
    val rdd: MongoRDD[Document] = MongoSpark.load(spark.sparkContext, readConfig)

    val values: RDD[String] = rdd.map(line => {
      val nObject: JSONObject = JSON.parseObject(line.toJson())

      nObject.remove("_id")
      nObject.remove("sku_id")
      nObject.remove("user_img")
      nObject.remove("Base_Info")
      nObject.remove("item_id")
      nObject.remove("user_name")

      nObject.toString
    })
    println(s"obs路径 : ${source_path}")
    spark.read.json(values).write.json(source_path)
  }
}
